const { request } = require("../util/util");
const getQiniuOldToken = async () => {
	const callBack = "jQuery33107664755328681041_1550477891715";
	if (
		global.QINIU_TOKEN &&
		new Date().getTime() - global.QINIU_TOKEN.expire < 60 * 1000
	) {
		return global.QINIU_TOKEN;
	}
	let result = await request({
		url: `http://common.zmwxxcx.com/upload/get-token?callback=${callBack}`,
		method: "GET",
		param: {
			callback: "jQuery33107664755328681041_1550477891715",
			LoginAdminID: 21
		},
		option: {
			string: true
		}
	});

	result = result.replace(callBack, "");
	result = result.replace(/^\(/, "");
	result = result.replace(/\)$/, "");
	result = JSON.parse(result.replace(callBack, ""));
	global.QINIU_TOKEN = result;
	global.QINIU_TOKEN.expire = new Date().getTime();
	return result;
};

module.exports = { getQiniuOldToken };
